﻿// This function is run when the app is ready to start interacting with the host application
// It ensures the DOM is ready before adding click handlers to buttons
Office.initialize = function (reason) {
    $(document).ready(function () {
        //Retrieve the currently selected data and place it in selected data text field
        $('#getDataBtn').click(function () { getData('#selectedDataTxt'); });

        //Retrieve the entire contents of the file
        $('#getFileContents').click(function () { getFileContents(); });

        // If setSelectedDataAsync method is supported by the host application
        // setDatabtn is hooked up to call the method else setDatabtn is removed
        if (Office.context.document.setSelectedDataAsync) {
            $('#setDataBtn').click(function () { setData('#selectedDataTxt'); });
        }
        else {
            $('#setDataBtn').remove();
        }

        if (Office.EventType.DocumentSelectionChanged) {
            //wire up an event handler for when the document selection changes
            Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, onDocSelectionChanged, addHandlerCallback);
        }
    });
};

// Writes data from textbox to the current selection in the document
function setData(elementId) {
    Office.context.document.setSelectedDataAsync($(elementId).val());
}

// Reads the data from current selection of the document and displays it in a textbox
function getData(elementId) {
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
    function (result) {
        if (result.status == 'succeeded') {
            $(elementId).val(result.value);
        }
    });
}


//functions for event handling when document selection changes
function onDocSelectionChanged(docSelectionChangedArgs) {
    write("Selection was changed.");
}

function addHandlerCallback(asyncResult) {
    write("<br>" + "Event handler initialized: selection change");
}

function write(message) {
    $('#message').append(message + "<br>");
}


//functions for retrieving file contents in 1 KB slices
function getFileContents() {
    if(Office.context.document.getFileAsync) {
        Office.context.document.getFileAsync(Office.FileType.Text, { sliceSize: 1000 }, function (asyncResult) {
            if (asyncResult.status == 'succeeded') {
                var myFile = asyncResult.value,
                    state = { file: myFile, counter: 0, sliceCount: myFile.sliceCount };

                getSliceData(state);
            }
        });
    }
    else {
        write("The host application does not support getting the entire file contents.");
    }
}

//Get 1 slice of data
function getSliceData(state) {
    state.file.getSliceAsync(state.counter, function (result) {
        var slice = result.value, data = slice.data;

        state.counter++;

        //Here we would typically do something with the data
        write("A slice of the file contents has been read: " + slice.data + "<br>");

        //Check to see if we're at our final slice - if not then get the next slice
        if (state.counter < state.sliceCount) {
            getSliceData(state);
        }
        else {
            closeFile(state);
        }
    });
}

//Close the file when we are done
function closeFile(state) {
    state.file.closeAsync(
        function (results) {
            write("Reading the file contents is complete.");
        });
}

